home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_13_11
/
plauger
/
complex.cpp
< prev
Wrap
C/C++ Source or Header
|
1995-09-11
|
19KB
|
610 lines
// complex standard header
#ifndef _COMPLEX_
#define _COMPLEX_
#include <cmath>
#include <istream>
_STD_BEGIN
#define __STD_COMPLEX
// TEMPLATE CLASS _Ctr
template<class _T> class _Ctr {
public:
static _T _Cosh(_T _X, _T _Y)
{return (::_Cosh((double)_X, (double)_Y)); }
static short _Exp(_T *_P, _T _Y, short _E)
{double _W = (double)*_P;
short _Ans = ::_Exp(&_W, (double)_Y, _E);
*_P = (_T)_W;
return (_Ans); }
static _T _Infv(_T)
{return (_Inf._D); }
static bool _Isinf(_T _X)
{double _W = (double)_X;
return (_Dtest(&_W) == _INF); }
static bool _Isnan(_T _X)
{double _W = (double)_X;
return (_Dtest(&_W) == _NAN); }
static _T _Nanv(_T)
{return (_Nan._D); }
static _T _Sinh(_T _X, _T _Y)
{return (::_Sinh((double)_X, (double)_Y)); }
static _T atan2(_T _Y, _T _X)
{return (::atan2((double)_Y, (double)_X)); }
static _T cos(_T _X)
{return (::cos((double)_X)); }
static _T exp(_T _X)
{return (::exp((double)_X)); }
static _T ldexp(_T _R, int _E)
{return (::ldexp((double)_R, _E)); }
static _T log(_T _X)
{return (::log((double)_X)); }
static _T pow(_T _X, _T _Y)
{return (::pow((double)_X, (double)_Y)); }
static _T sin(_T _X)
{return (::sin((double)_X)); }
static _T sqrt(_T _X)
{return (::sqrt((double)_X)); }
};
// CLASS _Ctr<float>
class _Ctr<float> {
public:
typedef float _T;
static _T _Cosh(_T _X, _T _Y)
{return (_FCosh(_X, _Y)); }
static short _Exp(_T *_P, _T _Y, short _E)
{return (_FExp(_P, _Y, _E)); }
static _T _Infv(_T)
{return (_FInf._F); }
static bool _Isinf(_T _X)
{return (_FDtest(&_X) == _INF); }
static bool _Isnan(_T _X)
{return (_FDtest(&_X) == _NAN); }
static _T _Nanv(_T)
{return (_FNan._F); }
static _T _Sinh(_T _X, _T _Y)
{return (_FSinh(_X, _Y)); }
static _T atan2(_T _Y, _T _X)
{return (atan2f(_Y, _X)); }
static _T cos(_T _X)
{return (cosf(_X)); }
static _T exp(_T _X)
{return (expf(_X)); }
static _T ldexp(_T _R, int _E)
{return (ldexpf(_R, _E)); }
static _T log(_T _X)
{return (logf(_X)); }
static _T pow(_T _X, _T _Y)
{return (powf(_X, _Y)); }
static _T sin(_T _X)
{return (sinf(_X)); }
static _T sqrt(_T _X)
{return (sqrtf(_X)); }
};
// CLASS _Ctr<double>
class _Ctr<double> {
public:
typedef double _T;
static _T _Cosh(_T _X, _T _Y)
{return (::_Cosh(_X, _Y)); }
static short _Exp(_T *_P, _T _Y, short _E)
{return (::_Exp(_P, _Y, _E)); }
static _T _Infv(_T)
{return (_Inf._D); }
static bool _Isinf(_T _X)
{return (_Dtest(&_X) == _INF); }
static bool _Isnan(_T _X)
{return (_Dtest(&_X) == _NAN); }
static _T _Nanv(_T)
{return (_Nan._D); }
static _T _Sinh(_T _X, _T _Y)
{return (::_Sinh(_X, _Y)); }
static _T atan2(_T _Y, _T _X)
{return (::atan2(_Y, _X)); }
static _T cos(_T _X)
{return (::cos(_X)); }
static _T exp(_T _X)
{return (::exp(_X)); }
static _T ldexp(_T _R, int _E)
{return (::ldexp(_R, _E)); }
static _T log(_T _X)
{return (::log(_X)); }
static _T pow(_T _X, _T _Y)
{return (::pow(_X, _Y)); }
static _T sin(_T _X)
{return (::sin(_X)); }
static _T sqrt(_T _X)
{return (::sqrt(_X)); }
};
// CLASS _Ctr<long double>
class _Ctr<long double> {
public:
typedef long double _T;
static _T _Cosh(_T _X, _T _Y)
{return (_LCosh(_X, _Y)); }
static short _Exp(_T *_P, _T _Y, short _E)
{return (_LExp(_P, _Y, _E)); }
static _T _Infv(_T)
{return (_LInf._L); }
static bool _Isinf(_T _X)
{return (_LDtest(&_X) == _INF); }
static bool _Isnan(_T _X)
{return (_LDtest(&_X) == _NAN); }
static _T _Nanv(_T)
{return (_LNan._L); }
static _T _Sinh(_T _X, _T _Y)
{return (_LSinh(_X, _Y)); }
static _T atan2(_T _Y, _T _X)
{return (atan2l(_Y, _X)); }
static _T cos(_T _X)
{return (cosl(_X)); }
static _T exp(_T _X)
{return (expl(_X)); }
static _T ldexp(_T _R, int _E)
{return (ldexpl(_R, _E)); }
static _T log(_T _X)
{return (logl(_X)); }
static _T pow(_T _X, _T _Y)
{return (powl(_X, _Y)); }
static _T sin(_T _X)
{return (sinl(_X)); }
static _T sqrt(_T _X)
{return (sqrtl(_X)); }
};
// TEMPLATE CLASS _Complex_base
template<class _T> class complex;
class complex<float>;
class complex<double>;
class complex<long double>;
template<class _T>
class _Complex_base {
public:
typedef _Complex_base<_T> _Myt;
_Complex_base(const _T& _R, const _T& _I)
: _Re(_R), _Im(_I) {}
#if _HAS_MEMBER_TEMPLATES
template class<U>
_Myt& operator=(const complex<_U>& _X)
{_Re = (_T)_X.real();
_Im = (_T)_X.imag();
return (*this); }
template<class _U>
_Myt& operator+=(const complex<_U>& _X)
{_Re += (_T)_X.real();
_Im += (_T)_X.imag();
return (*this); }
template<class _U>
_Myt& operator-=(const complex<_U>& _X)
{_Re -= (_T)_X.real();
_Im -= (_T)_X.imag();
return (*this); }
template<class _U>
_Myt& operator*=(const complex<_U>& _X)
{_T _Xre = (_T)_X.real();
_T _Xim = (_T)_X.imag();
_T _W = _Re * _Xre - _Im * _Xim;
_Im = _Re * _Xim + _Im * _Xre;
_Re = _W;
return (*this); }
template class<U>
_Myt& operator/=(const complex<_U>& _X)
{_T _Xre = (_T)_X.real();
_T _Xim = (_T)_X.imag();
if (_Ctr<_T>::_Isnan(_Xre) || _Ctr<_T>::_Isnan(_Xim))
_Re = _Ctr<_T>::_Nanv(_Xre), _Im = _Re;
else if ((_Xim < 0 ? -_Xim : +_Xim)
< (_Xre < 0 ? -_Xre : +_Xre))
{_T _Wr = _Xim / _Xre;
_T _Wd = _Xre + _Wr * _Xim;
if (_Ctr<_T>::_Isnan(_Wd) || _Wd == 0)
_Re = _Ctr<_T>::_Nanv(_Xre), _Im = _Re;
else
{_T _W = (_Re + _Im * _Wr) / _Wd;
_Im = (_Im - _Re * _Wr) / _Wd;
_Re = _W; }}
else if (_Xim == 0)
_Re = _Ctr<_T>::_Nanv(_Xre), _Im = _Re;
else
{_T _Wr = _Xre / _Xim;
_T _Wd = _Xim + _Wr * _Xre;
if (_Ctr<_T>::_Isnan(_Wd) || _Wd == 0)
_Re = _Ctr<_T>::_Nanv(_Xre), _Im = _Re;
else
{_T _W = (_Re * _Wr + _Im) / _Wd;
_Im = (_Im * _Wr - _Re) / _Wd;
_Re = _W; }}
return (*this); }
#else
_T real(_T _X)
{return (_Re = _X); }
_T imag(_T _X)
{return (_Im = _X); }
#endif
_T real() const
{return (_Re); }
_T imag() const
{return (_Im); }
private:
_T _Re, _Im;
};
// CLASS complex<float>
class complex<float> : public _Complex_base<float> {
public:
typedef float _T;
explicit complex(const complex<double>&);
explicit complex(const complex<long double>&);
complex(const _T& _R = 0, const _T& _I = 0)
: _Complex_base<_T>(_R, _I) {}
};
// CLASS complex<double>
class complex<double> : public _Complex_base<double> {
public:
typedef double _T;
complex(const complex<float>&);
explicit complex(const complex<long double>&);
complex(const _T& _R = 0, const _T& _I = 0)
: _Complex_base<_T>(_R, _I) {}
};
// CLASS complex<long double>
class complex<long double> : public _Complex_base<long double> {
public:
typedef long double _T;
complex(const complex<float>&);
complex(const complex<double>&);
complex(const _T& _R = 0, const _T& _I = 0)
: _Complex_base<_T>(_R, _I) {}
};
// CONSTRUCTORS FOR complex SPECIALIZATIONS
complex<float>::complex(const complex<double>& _X)
: _Complex_base<float>((_T)_X.real(), (_T)_X.imag()) {}
complex<float>::complex(const complex<long double>& _X)
: _Complex_base<float>((_T)_X.real(), (_T)_X.imag()) {}
complex<double>::complex(const complex<float>& _X)
: _Complex_base<double>((_T)_X.real(), (_T)_X.imag()) {}
complex<double>::complex(const complex<long double>& _X)
: _Complex_base<double>((_T)_X.real(), (_T)_X.imag()) {}
complex<long double>::complex(const complex<float>& _X)
: _Complex_base<long double>((_T)_X.real(), (_T)_X.imag()) {}
complex<long double>::complex(const complex<double>& _X)
: _Complex_base<long double>((_T)_X.real(), (_T)_X.imag()) {}
// TEMPLATE CLASS complex
template<class _T>
class complex : public _Complex_base<_T> {
public:
complex(const _T& _R = 0, const _T& _I = 0)
: _Complex_base<_T>(_R, _I) {}
#if _HAS_MEMBER_TEMPLATES
template<class _U>
#else
typedef _T _U;
#endif
complex(const complex<_U>& _X)
: _Complex_base<_T>((_T)_X.real(), (_T)_X.imag()) {}
};
#if !_HAS_MEMBER_TEMPLATES
// TEMPLATE complex OPERATORS
template<class _T, class _U> inline
complex<_T>& operator=(complex<_T>& _X, const complex<_U>& _Y)
{_X.real((_T)_X.real());
_X.imag((_T)_X.imag());
return (_X); }
template<class _T, class _U> complex<_T>& operator+=(
complex<_T>& _X,
const comp